package com.example.provider.service.Impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import com.example.common.model.ActivityArticle;
import com.example.common.model.condition.ActivityArticleCondition;
import com.example.provider.annotation.ThreadPoolStatus;
import com.example.provider.mapper.ActivityArticleMapper;
import com.example.provider.service.ActivityArticleService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import javax.annotation.Resource;

/**
 * 活动文章表Service接口实现
 *
 * @author 510
 * @version 1.0.0 2021-04-23
 */
@Slf4j
@Service
@Transactional(readOnly = true)
public class ActivityArticleServiceImpl extends ServiceImpl<ActivityArticleMapper, ActivityArticle> implements ActivityArticleService {
    private static final Logger logger = LoggerFactory.getLogger(ActivityArticleService.class);

    @Autowired
    ActivityArticleMapper activityArticleMapper;

    @Resource(name = "asyncServer")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Override
    public IPage<ActivityArticle> findActivityArticleByCondition(ActivityArticleCondition condition) {
        return null;
    }

//    @ThreadPoolStatus("t1--------------")
//    @Async("asyncServer")
    @Override
    public IPage<ActivityArticle> findActivityArticleByCondition(ActivityArticleCondition condition, Integer type) {

        IPage<ActivityArticle> page = condition.buildPage();
//        QueryWrapper<ActivityArticle> queryWrapper = condition.buildQueryWrapper(ActivityArticle.class);
        QueryWrapper<ActivityArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type).orderByDesc("aId");
        return this.page(page, queryWrapper);
    }

    /**
     * 查询特定type的所有信息
     *
     * @return lisy
     */
    public ArrayList<ActivityArticle> findActivityAllTypeList(Integer type){
        QueryWrapper<ActivityArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("type", type)
                .eq("deleteFlag", 1)
                .orderByDesc("aId");
        log.trace("查询列表");
        return (ArrayList<ActivityArticle>) activityArticleMapper.selectList(queryWrapper);
    }

    @ThreadPoolStatus("t3--------------")
    @Async("asyncServer")
    @Override
    public Future<ActivityArticle> getActivityArticleById(Integer aId) {
        return AsyncResult.forValue(this.getById(aId));
    }

//    @ThreadPoolStatus("t1--------------")
//    public ActivityArticle getActivityArticleById(Integer aId, Integer a) throws ExecutionException, InterruptedException {
//        Future<ActivityArticle> submit = threadPoolTaskExecutor.submit(new Callable<ActivityArticle>() {
//            @Override
//            public ActivityArticle call() throws Exception {
//                return ActivityArticleServiceImpl.this.getById(aId);
//            }
//        });
//        return submit.get();
//    }



    @Transactional
    @Override
    public boolean addActivityArticle(ActivityArticle activityArticle) {
        return this.save(activityArticle);
    }

    @Transactional
    @Override
    public boolean updateActivityArticle(ActivityArticle activityArticle) {
        return this.updateById(activityArticle);
    }

    @Transactional
    @Override
    public boolean deleteActivityArticleById(Integer aId) {
        return this.removeById(aId);
    }

    @Transactional
    @Override
    public boolean deleteActivityArticleByIds(List<Integer> idList) {
        return this.removeByIds(idList);
    }
}